﻿using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using Newtonsoft.Json;

namespace WebCommon.Api
{
    /// <summary>
    /// Api json 基类
    /// </summary> 
    public abstract class SubBase
    {
        protected string suidStr = "suid";
        protected string checkStr = "check";
        protected string dataStr = "data";

        
        #region Public Properties
        [JsonIgnore]
        public bool IsTest { get;protected set; }

        /// <summary>
        ///     验证
        /// </summary> 
        [DisplayName("加密后的md5")]
        [Required] 
        public string Check { get; set; }


        /// <summary>
        /// 内部消息 
        /// </summary>
        [JsonIgnore]
        public string Msg { get;protected set; }



        /// <summary>
        ///     是否通过验证
        /// </summary>
        protected bool IsValidate { get { return string.IsNullOrWhiteSpace(this.Msg); } }


        public object GetData(object subData)
        {
            return null;
        }
         
        /// <summary>
        ///     系统用户id
        /// </summary>
        [DisplayName("接口用户id")]
        [Required]
        public string Suid { get; set; }

        #endregion

        #region Public Methods and Operators



        /// <summary>
        /// 查看密钥是否正确
        /// </summary>
        /// <param name="key">
        /// </param>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        protected abstract bool IsSuids(string key);
           

        #endregion

        #region Methods



        /// <summary>
        /// Getjson
        /// </summary>
        /// <param name="fns">
        /// The fns.
        /// </param>
        /// <param name="md5">
        /// The md 5.
        /// </param>
        /// <returns>
        /// </returns> 
        public bool Validate(string md5)
        { 
            if (string.IsNullOrWhiteSpace(md5))
            { 
                return false;
            }

            if (this.IsValidate)
            {
                var isSuids = this.IsSuids(md5);
                if (!isSuids)
                {
                    return false;
                }

                return true;
            }
            else
            {
                return false;
            }
        } 
        #endregion


    }
}